package org.csu.mypetstore.controller;

import jakarta.servlet.http.HttpSession;
//import jakarta.websocket.Session;
import lombok.Getter;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.AdminCatalogService;
import org.csu.mypetstore.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class AdminItemController {
    @Autowired
    private ItemService itemService;
    @Getter
    @Autowired
    private AdminCatalogService adminCatalogService;

    //两大组件
    @GetMapping("/AdminEditItem")
    public String listItems(Model model) {
        model.addAttribute("items", adminCatalogService.listItems());
        return "admin/itemList";
    }

    @GetMapping("/AdminAddItem")
    public String editItem(@RequestParam(required = false) String itemId, Model model) {
        if (itemId != null) {
            Item item = adminCatalogService.getItem(itemId);
            model.addAttribute("item", item);

        } else {
            model.addAttribute("item", new Item());
        }
        model.addAttribute("products",adminCatalogService.listProducts());
        return "admin/editItem";
    }

    @PostMapping("/saveItem")
    public String saveItem(@ModelAttribute Item item,Model model,HttpSession session) {
        //System.out.println("Received productId: " + item.getProductId());  // 调试日志
        String productId = new String();
        productId= (String) model.getAttribute("productId");
        // 验证 productId 是否有效
        if (item.getItemId() == null || item.getItemId().isEmpty()) {
            session.setAttribute("message","item为空");
           return "/common/error";
        } else if (item.getProductId().isEmpty()) {
            session.setAttribute("message","product为空");
        } else if (itemService.SearchIsItemId(item.getItemId())) {//如果已经存在，则更新
            itemService.updateItem(item);
        } else {//不存在则添加
            itemService.addItem(item);
        }
        return "redirect:/catalog/AdminEditItem";
    }

    private boolean isValidProduct(String productId) {
        Product product = (Product) itemService.getProductById(productId);
        return product != null;
    }

    @GetMapping("AdminEditOneItem")
    public String AdminEditOneItem(Model model, String itemId,HttpSession session) {
        Item item = (itemId != null) ? adminCatalogService.getItem(itemId) : new Item();
        model.addAttribute("item", item); // 关键：始终传递 item 对象
        model.addAttribute("products", adminCatalogService.listProducts());
        session.setAttribute("OldItemEdit",itemId);
        return "admin/editoneItem";
    }

    @PostMapping("/saveoneEditItem")
    public String saveoneEditItem(@ModelAttribute Item item,Model model,HttpSession session ){       // 验证 productId 是否有效
        if (item.getItemId()== null || item.getItemId().isEmpty()) {
            session.setAttribute("message","item为空");
            return "/common/error";
        } else if (item.getProductId().isEmpty()) {
            session.setAttribute("message","product为空");
        }
        String OldItemEditId= (String) session.getAttribute("OldItemEdit");
        adminCatalogService.deleteItem(OldItemEditId);
        itemService.addItem(item);
        return "redirect:/catalog/AdminEditItem";
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam String itemId) {
        adminCatalogService.deleteItem(itemId);
        return "redirect:/catalog/AdminEditItem";
    }

}
